***************
* Title: gambia_ecd_edcc_table4.do
* Author: Todd Pugatch
* Description: replication code for Blimpo, Carneiro, Jervis, and Pugatch,
*	"Improving Access and Quality in Early Childhood Development Programs: 
*		Experimental Evidence from The Gambia"
*	for Economic Development and Cultural Change
* Inputs: ECD_3to6_Gambia_monitoring_cleanv1.dta
* Outputs: gambia_ecd_edcc_table4.txt, gambia_ecd_edcc_table4[a-b].xls
* Notes: creates Table 4
****************
#delimit;
local start=`"$S_TIME"';
clear;
clear matrix;
clear mata;
graph drop _all;
cap log close;
set more off;
/*set directory:
	cd mydir
*/
local data=`"Data\cleaned"';
local output=`"analysis\output"';

* Begin log file and open data;
log using `output'\gambia_ecd_edcc_table4.txt, text replace;
qui use `data'\ECD_3to6_Gambia_monitoring_cleanv1, clear;

* Sample characteristics;
/*overall sample size, number of facilitators interviewed at each site*/
qui egen site=tag(q104settlementcode);
qui gen x=1;
bysort q104settlementcode: egen numinsample=count(x);
lab var numinsample "number of facilitators interviewed at site";
qui drop if treatment==.;

/*observations*/
count;

/*sites*/
count if site==1;
tab numinsample if site==1;

/*number of observations by region and treatment status*/
table treatment, c(freq rawsum site);
table treatment q101region, c(freq rawsum site);

* Facilitator characteristics;
/*report as balance table between community-based and ECD Annex treatments (no control group from either experiment was monitored)*/
qui gen region2=(q101region==2);
local X "region2 q202female q203age q205edulevel q205edulevel_primary_ormore q205edulevel_lowsec_ormore
	q205edulevel_sec_ormore q207qualified q210teachexperience q211ECDexperience q212ECDlength q213citizen 
	q214livelocal q215commutetime q216married q217havekids";
local absence "a201workhrs a202absent";
local syllabus "q304nametheme q305followsyllabus_full";
local involve "q401a_verymuch q401b_verymuch q401c_verymuch q401d_verymuch q401e_verymuch";	

/*unadjusted means & differences by treatment status*/
/*account for correlated outcomes by settlement by clustering at that level?*/
orth_out `X' `absence' `syllabus' `involve' using `output'\gambia_ecd_edcc_table4a.xls, 
	by(treatment) se vce(cluster settlement_code) compare count colnum 
	title("teacher characteristics, unadjusted means") replace;

/*adjusted means, adjusting for regional stratification*/ 
orth_out `X' `absence' `syllabus' `involve' using `output'\gambia_ecd_edcc_table4a.xls, 
	by(treatment) se vce(cluster settlement_code) compare test count colnum covar(region2) 
	title("teacher characteristics, adjusted means") happend replace;
	
* Site characteristics;
qui gen nt=1;
qui gen ntf=(q202female==1);
qui gen ntm=(q202female==0);
qui gen qt=(q207qualified==1);
qui gen qtf=(q202female==1 & q207qualified==1);
qui gen qtm=(q202female==0 & q207qualified==1);
local sumvars "nt ntf ntm qt qtf qtm";
local meanvars "a205registedboy a206registedgirl registered_children a207presentboy a208presentgirl present_children
	treatment region2"; 
qui collapse (rawsum) `sumvars' (mean) `meanvars', by(q104settlementcode);	
qui gen presentboy_pct=a207presentboy/a205registedboy;
qui gen presentgirl_pct=a208presentgirl/a206registedgirl;
qui gen present_children_pct=present_children/registered_children;
qui gen ptr=registered_children/nt;
qui gen pqtr=registered_children/qt;
qui gen qtp=qt/registered_children;

lab var a205registedboy "number of registered boys";
lab var a206registedgirl "number of registered girls";
lab var a207presentboy "number of boys present on day of visit";
lab var a208presentgirl "number of girls present on day of visit";
lab var registered_children "number of registered children";
lab var present_children "number of children present on day of visit";
lab var presentboy_pct "% of registered boys present on day of visit";
lab var presentgirl_pct "% of registered girls present on day of visit";
lab var present_children_pct "% of registered children present on day of visit";
lab var nt "number of teachers";
lab var ntf "number of female teachers";
lab var ntm "number of male teachers";
lab var qt "number of qualified teachers";
lab var qtf "number of qualified female teachers";
lab var qtm "number of qualified male teachers";
lab var ptr "pupil-teacher ratio";
lab var pqtr "pupil-qualified teacher ratio";
lab var qtp "qualified teacher-pupil ratio";

/*balance (remove pupil-qualified teacher ratio because too many missings due to 0 qualified teachers*/
local X "nt ntf ntm qt qtf qtm a205registedboy a206registedgirl registered_children a207presentboy a208presentgirl 
	present_children presentboy_pct presentgirl_pct present_children_pct ptr qtp";
orth_out `X' using `output'\gambia_ecd_edcc_table4b.xls, by(treatment) se compare count colnum 
	title("site characteristics, unadjusted means") replace;
orth_out `X' using `output'\gambia_ecd_edcc_table4b.xls, by(treatment) se compare test count colnum covar(region2) 
	title("site characteristics, adjusted means") happend replace;	

local end=`"$S_TIME"'; 
di "`start'";
di "`end'";
log close;
